package com.cc.micro.common.web;


import com.cc.micro.common.enumeration.HttpCode;
import lombok.Data;

/**
 * @author CC
 * time 2023-07-01
 * description
 */
@Data
public class Result<T> {

    private T data;
    private String msg;
    private Integer code;

    public static<T> Result ok(){
        Result<T> r = new Result<>();
        r.setCode(HttpCode.OK.getCode());
        r.setMsg(HttpCode.OK.getMsg());
        r.setData(null);
        return r;
    }
    public static<T> Result build(T data,String msg,Integer code){
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }
    public static<T> Result build(T data, HttpCode code){
        Result<T> r = new Result<>();
        r.setData(data);
        r.setCode(code.getCode());
        r.setMsg(code.getMsg());
        return r;
    }
    public static <T> Result fail(HttpCode code){
        return fail(code.getMsg(),code.getCode());
    }
    public static<T> Result ok(T data){
        Result<T> r = new Result<>();
        r.setCode(HttpCode.OK.getCode());
        r.setMsg(HttpCode.OK.getMsg());
        r.setData(data);
        return r;
    }
    public static<T> Result ok(T data,String msg){
        Result<T> r = new Result<>();
        r.setCode(HttpCode.OK.getCode());
        r.setMsg(msg);
        r.setData(data);
        return r;
    }
    public static<T> Result fail(){
        Result<T> r = new Result<>();
        r.setCode(HttpCode.ERROR.getCode());
        r.setMsg(HttpCode.ERROR.getMsg());
        r.setData(null);
        return r;
    }
    public static<T> Result fail(String msg){
        Result<T> r = new Result<>();
        r.setCode(HttpCode.ERROR.getCode());
        r.setMsg(msg);
        r.setData(null);
        return r;
    }
    public static<T> Result fail(String msg,Integer code){
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(null);
        return r;
    }
    public Result<T> msg(String msg){
        this.msg=msg;
        return this;
    }
    public Result<T> code(Integer code){
        this.code=code;
        return this;
    }
    public Result<T> data(T data){
        this.data=data;
        return this;
    }

}
